package cn.itsource.org.config;

import cn.hutool.core.exceptions.ValidateException;

import cn.itsource.org.util.JSONResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @ControllerAdvice:就是把自定义的异常捕获掉,并且返回到Controller
 * @RestControllerAdvice:
 */
//@ControllerAdvice 1.@RestControllerAdvice
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {




    /**
     * 请求方式不支持
     * 2.@ExceptionHandler
     */
    @ExceptionHandler({HttpRequestMethodNotSupportedException.class})
    public JSONResult handleException(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return JSONResult.error("不支持' " + e.getMethod() + "'请求");
    }

    /**
     * 拦截未知的运行时异常
     */
    @ExceptionHandler(RuntimeException.class)
    public JSONResult notFount(RuntimeException e) {
        //log.error("运行时异常:", e);
        return JSONResult.error(e.getMessage());
    }

    /**
     * 系统异常
     */
//    @ExceptionHandler(Exception.class)
//    public JSONResult handleException(Exception e) {
//        log.error(e.getMessage(), e);
//        return JSONResult.error("服务器错误，请联系管理员");
//    }
    @ExceptionHandler(value = ArithmeticException.class)
    public String ArithmeticException(ArithmeticException e){
        e.getStackTrace();
        return  e.getMessage();
    }

    /**
     * 校验异常
     * ArithmeticException
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public JSONResult exceptionHandler(MethodArgumentNotValidException e) {
        BindingResult bindingResult = e.getBindingResult();
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getDefaultMessage() + "!";
        }
        return JSONResult.error(errorMessage);
    }

    /**
     * 校验异常
     */
    @ExceptionHandler(value = BindException.class)
    public JSONResult validationExceptionHandler(BindException e) {
        BindingResult bindingResult = e.getBindingResult();
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getDefaultMessage() + "!";
        }
        return JSONResult.error(errorMessage);
    }

    /**
     * 校验异常
     */
    @ExceptionHandler(value = ConstraintViolationException.class)
    public JSONResult ConstraintViolationExceptionHandler(ConstraintViolationException ex) {
        Set<ConstraintViolation<?>> constraintViolations = ex.getConstraintViolations();
        Iterator<ConstraintViolation<?>> iterator = constraintViolations.iterator();
        List<String> msgList = new ArrayList<>();
        while (iterator.hasNext()) {
            ConstraintViolation<?> cvl = iterator.next();
            msgList.add(cvl.getMessageTemplate());
        }
        return JSONResult.error(String.join(",",msgList));
    }

    /**
     * 糊涂工具类的校验异常
     * @param e
     * @return
     */
    @ExceptionHandler(ValidateException.class)
    public JSONResult ValidateException(ValidateException e) {
        log.error(e.getMessage(), e);
        return JSONResult.error(e.getMessage());
    }
    /**
     * 业务异常
     */
//    @ExceptionHandler(BusinessException.class)
//    public JSONResult businessException(BusinessException e) {
//        log.error(e.getMessage(), e);
//        return JSONResult.error(e.getMessage());
//    }


}